package zifuchuan;

import java.util.HashSet;
import java.util.Set;

public class 无重复字符的最长子串3 {

    public static void main(String[] args) {

        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }

    public static int lengthOfLongestSubstring(String s) {
        int res = 0;

        Set<Character> set = new HashSet<>();

        for (int i = 0, j = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            // 如果set中有重复的了
            while (set.contains(c)) {
                char c1 = s.charAt(j++);
                set.remove(c1);
            }
            set.add(c);
            res = Math.max(res, i - j + 1);
        }
        return res;
    }
}
